package com.zz.hhbl.ui.translate

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.zz.framework.core.ext.onClick
import com.zz.hhbl.R
import com.zz.hhbl.databinding.ItemTranslateLeftTestBinding
import com.zz.hhbl.databinding.ItemTranslateRightTestBinding
import com.zz.hhbl.entity.TranslateEntity

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/1 20:10.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class TestTranslateAdapter : BaseMultiItemAdapter<TranslateEntity>() {
    private val ITEM_TYPE_LEFT = 9998
    private val ITEM_TYPE_RIGHT = 9999

    private class ItemTranslateLeftViewHolder(val viewBinding: ItemTranslateLeftTestBinding) : RecyclerView.ViewHolder(viewBinding.root)
    private class ItemTranslateRightViewHolder(val viewBinding: ItemTranslateRightTestBinding) : RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(ITEM_TYPE_LEFT, object : OnMultiItemAdapterListener<TranslateEntity, ItemTranslateLeftViewHolder> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemTranslateLeftViewHolder {
                val viewBinding = ItemTranslateLeftTestBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemTranslateLeftViewHolder(viewBinding)
            }
            override fun onBind(holder: ItemTranslateLeftViewHolder, position: Int, item: TranslateEntity?) {
                holder.viewBinding.apply {
                    tvContent.apply {
                        onClick {
                            item?.let {
                                mListener?.invoke(item)
                            }
                        }
                        text = item?.text.orEmpty()
                        (parent as ViewGroup).clipChildren = false
                    }
                    tvStatus.setText(when (item?.status) {
                        0 -> "处理中"
                        1 -> "点击播放"
                        2 -> "翻译失败"
                        else -> ""
                    })
                    tvStatus.setTextColor(tvStatus.context.getColor(
                        when (item?.status) {
                            0 -> R.color.color_orange
                            1 -> R.color.color_green
                            2 -> R.color.color_red
                            else -> R.color.color_orange
                        }
                ))
                }

            }
        }).addItemType(ITEM_TYPE_RIGHT, object : OnMultiItemAdapterListener<TranslateEntity, ItemTranslateRightViewHolder> {
            override fun onBind(holder: ItemTranslateRightViewHolder, position: Int, item: TranslateEntity?) {
                holder.viewBinding.tvContent.apply {
                    onClick {
                        item?.let {
                            mListener?.invoke(item)
                        }
                    }
                    text = item?.text.orEmpty()
                    (parent as ViewGroup).clipChildren = false
                }
            }
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemTranslateRightViewHolder {
                val viewBinding = ItemTranslateRightTestBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemTranslateRightViewHolder(viewBinding)
            }
        }).onItemViewType { position, list ->
            if (list[position].isUserVoice) {
                ITEM_TYPE_RIGHT
            } else {
                ITEM_TYPE_LEFT
            }
        }
    }

    var mListener: ((item: TranslateEntity) -> Unit)? = null
    fun setItemClickListener(listener: (item: TranslateEntity) -> Unit) :TestTranslateAdapter{
        mListener = listener
        return this
    }

    fun getData(): MutableList<TranslateEntity> = items.toMutableList().apply { items = this }


    override fun submitList(list: List<TranslateEntity>?) {
        getData().clear()
        if (list.isNullOrEmpty().not()) {
            getData().addAll(list)
        }
        super.submitList(list)
    }

    fun addOrUpdateData(item: TranslateEntity) {
        if (getData().isNullOrEmpty().not()) {
            if (getData().find { it.taskId == item.taskId } == null) {
                getData().add(item)
            } else {
                getData().map {
                    if (it.taskId == item.taskId) {
                        it.status = item.status
                        it.text = item.text
                        it.url = item.url
                    }
                }
            }
        } else {
            getData().add(item)
        }
        submitList(getData())
    }

}